package com.example.yls.demoa;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private  Button btnsave;
private Button btnread;
private EditText name;
private EditText age;
private RadioButton isBoy;
private RadioButton isGirl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnsave=findViewById(R.id.btn_save);
        btnsave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp=getSharedPreferences("student", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("isBoy",true);
                editor.putString("name","黎明");
                editor.putInt("age",50);
                editor.commit();
            }
        });
        btnread=findViewById(R.id.btn_raed);
        btnread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp=getSharedPreferences("student",Context.MODE_PRIVATE);
                String name=sp.getString("name",null);
                Boolean isBoy=sp.getBoolean("isBoy",true);
                int age=sp.getInt("age",0);
                Toast.makeText(MainActivity.this,name+"  "+age+" "+isBoy,Toast.LENGTH_LONG).show();
            }
        });
    }
    protected void onStart(){
        readInfoFromSP();
        super.onStart();

    }

    private void readInfoFromSP() {
        SharedPreferences sp=getSharedPreferences("student",Context.MODE_PRIVATE);
        String name=sp.getString("name",null);
        Boolean isBoy=sp.getBoolean("isBoy",true);
        int age=sp.getInt("age",0);
    }

    protected void onStop(){
        saveIntoSP();
        super.onStop();
    }

    private void saveIntoSP() {
        SharedPreferences sp=getSharedPreferences("student", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        
        editor.putBoolean("isBoy",true);
        editor.putString("name","黎明");
        editor.putInt("age",50);
        editor.commit();
    }
}
